package com.core.gig.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.core.as.ASException;
import com.core.gig.as.ASConfiguracao;
import com.core.gig.as.ASFactory;
import com.core.gig.dto.NHPDTO;
import com.core.gig.util.GIGUtil;

/**
 * @author Lucas Ceni
 *
 */
public class NHPAction
		extends GIGAction
{
	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		return inicial(mapping, form, request, response);
	}

	public ActionForward inicial(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		clearForm(form);
		try
		{
			if (request.getSession().getAttribute("nhp") == null)
			{
				ASConfiguracao as = ASFactory.getInstance().getASConfiguracao();
				NHPDTO nhp = as.recuperarNHPPadrao();
				request.setAttribute("nhp", nhp);
			}
		}
		catch (ASException e)
		{
			addMessage(request, e.getMessage(), ICO_ERROR);
			return mapping.findForward(FWD_POPUP_ERROR);
		}
		
		return mapping.findForward(FWD_DEFAULT);
	}
	
	public ActionForward padrao(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		try
		{
				ASConfiguracao as = ASFactory.getInstance().getASConfiguracao();
				NHPDTO nhp = as.recuperarNHPPadrao();
				request.setAttribute("nhp", nhp);
				dyna(form).set("padrao", Boolean.TRUE);
		}
		catch (ASException e)
		{
			addMessage(request, e.getMessage(), ICO_ERROR);
			return mapping.findForward(FWD_POPUP_ERROR);
		}
		
		return mapping.findForward(FWD_DEFAULT);
	}
	
	public ActionForward calcular(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		NHPDTO nhp = new NHPDTO();
		GIGUtil.copyProperties(nhp, form);
		
		try
		{
			ASConfiguracao as = ASFactory.getInstance().getASConfiguracao();
			nhp = as.calcularNHP(nhp);
			request.setAttribute("nhp", nhp);
		}
		catch (ASException e)
		{
			addMessage(request, e.getMessage(), ICO_ERROR);
		}
		
		return mapping.findForward(FWD_DEFAULT);
	}
	
	public ActionForward salvar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		NHPDTO nhp = new NHPDTO();
		GIGUtil.copyProperties(nhp, form);
		
		boolean padrao = (Boolean) dyna(form).get("padrao");
		
		if (padrao)
		{
			try
			{
				ASConfiguracao as = ASFactory.getInstance().getASConfiguracao();
				as.salvarNHPPadrao(nhp);
			}
			catch (ASException e)
			{
				addMessage(request, e.getMessage(), ICO_ERROR);
				return mapping.findForward(FWD_POPUP_ERROR);
			}
		}
		else
		{
			request.getSession().setAttribute("nhp", nhp);
		}
		return null;
	}
	
	public ActionForward restaurar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		request.getSession().removeAttribute("nhp");
		return inicial(mapping, form, request, response);
	}
}